package com.ruan.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruan.entity.Cart;
import com.ruan.entity.Result;
import com.ruan.query.CartQuery;
import com.ruan.service.CartService;
import com.ruan.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName:CartController
 * @Author:阮昇
 * @Description:
 * @Datetime: 2023/7/2 19:39
 **/
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @PostMapping("/addCart")
    public Result addCart(Cart cart){
        return cartService.addCart(cart);
    }

    @PostMapping("/getCartList")
    public Result getCartList(CartQuery cartQuery){
        //开启分页
        PageHelper.startPage(cartQuery.getPageNum(),cartQuery.getPageSize());

        List<CartVo> cartVoList = cartService.getCartList(cartQuery);

        //当前页，下一页，上一页，首页，尾页。。。。需要一个类进行封装，
        PageInfo<CartVo> cartVoPageInfo = new PageInfo<>(cartVoList);

        return new Result(200,cartVoPageInfo);
    }

    @RequestMapping("/deleteCart/{id}")
    public Result deleteCart(@PathVariable Integer id){
        return cartService.deleteCart(id);
    }

    @PostMapping("/deleteCartByIds")
    public Result deleteCartByIds(@RequestParam("ids[]") Integer[] ids){
        return cartService.deleteCartByIds(ids);
    }

    @PostMapping("/editCart")
    public Result editCart(Cart cart){
        return cartService.editCart(cart);
    }

    @RequestMapping("/deleteCartByUserId/{id}")
    public void deleteCartByUserId(@PathVariable Integer id){
        cartService.deleteCartByUserId(id);
    }

    @PostMapping("/deleteCartByUserIds")
    public void deleteCartByUserIds(@RequestBody Integer[] ids){
        cartService.deleteCartByUserIds(ids);
    }
}
